	<?php $this->load->view('admin/partials/feed_menu'); ?>
	
	<?php 
		$mode = isset($_GET['mode']) ? $this->input->get('mode') : 'Livescore';
		$teamArray = $this->feed->getArrayTeams($mode);
		$teamFeedID = array();
		foreach($this->team_entity->getArrayListOfTeam() as $item){
			$teamFeedID[] = $item->feed_team_id;
		}
		 
	?>
	
	<div class="clrboth"></div>
	<h3>Team Explore</h3>
	<div class="clrboth"></div>
	<div style="float:left;">
		<form action="" method="get" id="changemod-form">
			<select id="select-mode" name="mode">
				<option value="Livescore" <?php if($mode == 'Livescore') echo "selected='selected'";?>>Live Scores Teams</option>
				<option value="Fixtures" <?php if($mode == 'Fixtures') echo "selected='selected'";?>>Fixtures Teams</option>
			</select>
		</form>
	</div>
	
	<div style="float:left;margin-left:50px;">
		<a href="javascript:void(0);" onclick="return clearCacheTeam();">Refresh</a>
		<?php echo loader_image('id="loader_image_refresh" class="hidden"');?>
	</div>
	
	<table border="0" class="table-list">
		<thead>
			<tr>
				<th width="20%">Feed Team ID</th>
				<th width="60%">Team</th>
				<th width="20%">Action</th>
			</tr>
		</thead>
		
		<tbody>
			<?php foreach($teamArray as $key=>$val):?>
				<tr>
					<td><?php echo $key;?></td>
					<td><?php echo $val;?></td>
					<td>
						<?php if(!in_array($key,$teamFeedID)):?>
							<a href="javascript:void(0);" onclick="return addTeamToDb(<?php echo $key;?>,'<?php echo addslashes($val);?>');">Add Team</a>
						<?php endif;?>	
					</td>
				</tr>
			<?php endforeach;?>
		</tbody>
	</table>

<script type="text/javascript">
	jQuery(document).ready(function() {
		jQuery( "#select-mode" ).bind('change',function(){
			jQuery('#changemod-form').submit();
		});	
		
		var options = { 
			beforeSubmit:function(formData, jqForm, options){ 
							return true;
						},   
			success:function(responseText, statusText, xhr, $form){
				if(responseText == 'ok'){
					reload();
				}else{
					alert(responseText);
				}
			}   
		};	
		
		jQuery("#add_team_submit").live("submit",function(e){
			e.preventDefault(); // <-- important
			jQuery(this).ajaxSubmit(options);
			return false;
		});
	});
	
	function clearCacheTeam(){
		jQuery('#loader_image_refresh').show();
		jQuery.post(
			BASE_URI+'admin/dataman/feed/clearCacheTeam',{},function(){
				jQuery('#loader_image_refresh').hide();
			}
		);
	}
	
	function addTeamToDb(team_id, name){
		jQuery.post(BASE_URI+'admin/dataman/general/add_team',{},function(res){
			jQuery('#dialog').html(res);
			jQuery('#dialog').dialog(
				{
					 width: 650,
					 resizable: false,
					 height:200,
					 title: 'Add team' 
				}
			);
			jQuery('#dialog #name').attr('value',name);
			jQuery('#dialog #feed_team_id').attr('value',team_id);
		});
	}
</script>	

